package org.smart4j.framework.helper;

/**
 * Created by liangyue on 2018/1/13.
 */


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * servlet助手类
 */
public final class ServletHelper {
	private static final Logger LOGGER = LoggerFactory.getLogger(ServletHelper.class);

	/**
	 * 使每个线程独自拥有一份ServletHelper实例
	 */
	private static final ThreadLocal<ServletHelper> SERVLET_HELPER_HOLDER = new ThreadLocal<>();

	private HttpServletRequest request;
	private HttpServletResponse response;

	private ServletHelper(HttpServletRequest request, HttpServletResponse response) {
		this.request = request;
		this.response = response;
	}

	/**
	 * 初始化
	 * @param request
	 * @param response
	 */
	public static void init(HttpServletRequest request, HttpServletResponse response){
		SERVLET_HELPER_HOLDER.set(new ServletHelper(request, response));
	}

	/**
	 * 销毁
	 */
	public static void destroy(){
		SERVLET_HELPER_HOLDER.remove();
	}

	/**
	 * 获取Request对象
	 * @return
	 */
	private static HttpServletRequest getRequest(){
		return SERVLET_HELPER_HOLDER.get().request;
	}

	/**
	 * 获取Response对象
	 * @return
	 */
	private static HttpServletResponse getResponse(){
		return SERVLET_HELPER_HOLDER.get().response;
	}

	/**
	 * 获取Session对象
	 * @return
	 */
	private static HttpSession getSession(){
		return getRequest().getSession();
	}

	/**
	 * 获取ServletContext对象
	 * @return
	 */
	private static ServletContext getServletContext(){
		return getRequest().getServletContext();
	}

	/**
	 * 将属性放入Request中
	 * @param key
	 * @param value
	 */
	public static void setRequestAttribute(String key, Object value){
		getRequest().setAttribute(key, value);
	}

	/**
	 * 从Request中获取属性值
	 * @param key
	 * @param <T>
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static <T> T getRequestAttribute(String key){
		return (T)getRequest().getAttribute(key);
	}

	/**
	 * 从Request中移除属性
	 * @param key
	 */
	public static void removeRequestAttribute(String key){
		getRequest().removeAttribute(key);
	}

	/**
	 * 发出重定向响应
	 * @param location
	 */
	public static void sendReDirect(String location){
		try {
			getResponse().sendRedirect(getRequest().getContextPath()+location);
		} catch (IOException e) {
			LOGGER.error("redirect failure", e);
		}
	}

	/**
	 * 将属性放入Session中
	 * @param key
	 * @param value
	 */
	public static void setSessionAttribute(String key, String value){
		getSession().setAttribute(key, value);
	}

	/**
	 * 从session中获取属性
	 * @param key
	 * @param <T>
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static <T> T getSessionAttribute(String key){
		return (T) getRequest().getSession().getAttribute(key);
	}

	/**
	 * 从Session中移除属性
	 * @param key
	 */
	public static void removeSessionAttribute(String key){
		getRequest().getSession().removeAttribute(key);
	}

	/**
	 * 使Session失效
	 */
	public static void invaliateSession(){
		getRequest().getSession().invalidate();
	}

	public static String getRequestPath(){
		return getRequest().getPathInfo().toString();
	}
}
